//
//  CYNewsCell.m
//  cyjf
//
//  Created by tauCross on 2017/7/6.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYNewsCell.h"

#import <UIImageView+WebCache.h>



@implementation CYNewsCellObject

@end



@interface CYNewsCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *sourceLabel;
@property(nonatomic, strong)UILabel *dateLabel;
@property(nonatomic, strong)UIImageView *thumbImageView;

@end


@implementation CYNewsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        self.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        self.titleLabel.numberOfLines = 2;
        [self.contentView addSubview:self.titleLabel];
        
        self.sourceLabel = [[UILabel alloc] init];
        self.sourceLabel.textColor = HEXCOLOR(@"999999");
        self.sourceLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.sourceLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = HEXCOLOR(@"999999");
        self.dateLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.dateLabel];
        
        self.thumbImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 65)];
        self.thumbImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.thumbImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:self.thumbImageView];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 105;
}

- (BOOL)shouldUpdateCellWithObject:(CYNewsCellObject *)object
{
    self.titleLabel.text = object.title;
    
    self.sourceLabel.text = object.source;
    
    self.dateLabel.text = object.date;
    
    [self.thumbImageView sd_setImageWithURL:object.thumb.tc_url placeholderImage:APP_SQUARE_PLACEHOLDER];
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.size = [self.titleLabel sizeThatFits:CGSizeMake(self.contentView.width - 160, MAXFLOAT)];
    self.titleLabel.left = 20;
    self.titleLabel.top = 20;
    
    [self.sourceLabel sizeToFit];
    self.sourceLabel.left = 20;
    self.sourceLabel.centerY = 79;
    
    [self.dateLabel sizeToFit];
    self.dateLabel.left = self.sourceLabel.right + 10;
    self.dateLabel.centerY = self.sourceLabel.centerY;
    
    self.thumbImageView.centerY = self.contentView.height / 2;
    self.thumbImageView.right = self.contentView.width - 20;
}

@end
